% encode(+L1,-L2).

encode([],[]):-!.
encode([H|T],L2):- 
    encode(T,L2,H,1,[]).


encode([],L2,H,Counter,Acc):-!,
    create_sub_list(H,Counter,R),
    append(Acc,[R],Acc1),
    L2 = Acc1.
encode([X|T],L2,H,Counter,Acc):-
    X == H,!,
    Counter1 is Counter + 1,
    encode(T,L2,X,Counter1,Acc).
encode([X|T],L2,H,Counter,Acc):-
    X \= H,!,
    create_sub_list(H,Counter,R),
    append(Acc, [R], Acc1),
    encode(T,L2,X,1,Acc1).
    
create_sub_list(H,Counter,R):-
    Counter == 1,!,
    R = H.
create_sub_list(H,Counter,R):-
    Counter > 1,!,
    R = [Counter,H].    


